﻿namespace TY.SoketClient
{
    using System;
    using DotNetty.Transport.Channels;
    using MO.Protocol;

    public class ClientHandler : SimpleChannelInboundHandler<BasicMessage>
    {
        private PackageManager packageManager;

        public ClientHandler(PackageManager packageManager)
        {
            this.packageManager = packageManager;
        }

        protected override void ChannelRead0(IChannelHandlerContext contex, BasicMessage msg) {
            packageManager.Push(msg);
            Console.WriteLine(msg);
        } 

        public override void ExceptionCaught(IChannelHandlerContext contex, Exception e)
        {
            
            Console.WriteLine(DateTime.Now.Millisecond);
            Console.WriteLine(e.StackTrace);
            contex.CloseAsync();
        }
    }
}